<?php
 /**
* Yii extension implementing fcbkListSelection v1.1
*  fcbkListSelection is fancy item selector
*  Coded by: emposha
*  See : http://www.emposha.com
*  Copyright: Emposha.com
*  email: admin@emposha.com
*  Licences: MIT, GPL
*
*  This widget is a wrapper for the above jquery plugin
*
* @package FcbkList
* @author Locke
* @version 0.1
* @filesource FcbkList.php
*
*/
class FcbkList extends CWidget {

    public $listData;
    public $options = array();
    private $_html;
    private $_baseUrl;

  /**
   * Execute the widget
   */
  public function run() {
    if(!isset ($this->listData)) {
      throw new CException("List source is not set or it's empty");
    }
    $this->registerClientScripts();
    $this->createMarkup();
  }

  /**
   * Registers the clientside widget files (css & js)
   */
  public function registerClientScripts() {
  // Get the resources path
    $resources = dirname(__FILE__).DIRECTORY_SEPARATOR.'resources';
    // publish the files
    $this->_baseUrl = Yii::app()->assetManager->publish($resources);
    Yii::app()->clientScript->registerScriptFile($this->_baseUrl.'/js/fcbklistselection.min.js');
    Yii::app()->clientScript->registerCssFile($this->_baseUrl.'/css/fcbklistselection.css');
  }

  /**
   * Creates the html markup needed by the widget
   */
    public function createMarkUp() {
        Yii::app()->clientScript->registerScript('fcbklist',
                '$(document).ready(function() {
                    $.fcbkListSelection("#'.$this->options['id'].'","'.$this->options['width'].'","'.$this->options['height'].'","'.$this->options['row'].'");
                    });'
                , CClientScript::POS_END);
       $this->makeOptions();
       // $this->setListOptions();
        $this->_html.='<ul id="'.$this->options['id'].'">';
        // Create list items
        foreach ($this->listData as $index=>$item){
            $this->_html.='<li>';
            $this->_html.='<strong>'.$item.'</strong>';
            $this->_html.='<input type="hidden" name="'.$this->options['name'].'[]" value="'.$index.'" />';
            $this->_html.='</li>';
        }
        $this->_html.='</ul>';
        echo $this->_html; // Final Html Output to view
    }


    /*
     * makeOptions sets default values to option not defined by user
     */
    private function makeOptions()
        {
            // Set defaults
            $this->setOption("id","fcbklist");
            $this->setOption("name","fcbklist_value");
            $this->setOption("width",400);
            $this->setOption("height",20);
            $this->setOption("row",2);

        }
    /*
     * Set Gallery Options
     */
    private function setGalleryOptions(){
        $this->_html.="<script type=\"text/javascript\">";
        $this->_html.="$(function(){";
        $this->_html.="$.fn.mbGallery.defaults={";
        $this->_html.="containment:\"".$this->options['containment']."\",";
        $this->_html.="galleryTop:".$this->options['galleryTop'].",";
        $this->_html.="galleryWidth:".$this->options['galleryWidth'].",";
        $this->_html.="galleryHeight:".$this->options['galleryHeight'].",";
        $this->_html.="galleryMaxWidth:".$this->options['galleryMaxWidth'].",";
        $this->_html.="galleryMaxHeight:".$this->options['galleryMaxHeight'].",";
        $this->_html.="galleryColor:\"".$this->options['galleryColor']."\",";
        $this->_html.="galleryFrameBorder:".$this->options['galleryFrameBorder'].",";
        $this->_html.="galleryFrameColor: \"".$this->options['galleryFrameColor']."\",";
        $this->_html.="maskOpacity:".$this->options['maskOpacity'].",";
        $this->_html.="maskBgnd:\"".$this->options['maskBgnd']."\",";
        $this->_html.="startFrom: \"".$this->options['startFrom']."\",";
        $this->_html.="headerOpacity:".$this->options['headerOpacity'].",";
        $this->_html.="thumbsBorder:".$this->options['thumbsBorder'].",";
        $this->_html.="thumbHeight:".$this->options['thumbHeight'].",";
        $this->_html.="thumbStripWidth:".$this->options['thumbStripWidth'].",";
        $this->_html.="thumbStripColor: \"".$this->options['thumbStripColor']."\",";
        $this->_html.="thumbStripPos: \"".$this->options['thumbStripPos']."\",";
        $this->_html.="thumbSelectColor: \"".$this->options['thumbSelectColor']."\",";
        $this->_html.="thumbOverColor: \"".$this->options['thumbOverColor']."\",";
        $this->_html.="imageSelector: \".imgFull\",";
        $this->_html.="thumbnailSelector: \".imgThumb\",";
        $this->_html.="descSelector: \".imgDesc\",";
        $this->_html.="descriptionWidth:".$this->options['descriptionWidth'].",";
        $this->_html.="labelColor: \"".$this->options['labelColor']."\",";
        $this->_html.="labelColorDisactive: \"".$this->options['labelColorDisactive']."\",";
        $this->_html.="labelTextColor: \"".$this->options['labelTextColor']."\",";
        $this->_html.="labelTextSize: \"".$this->options['labelTextSize']."\",";
        $this->_html.="labelHeight:".$this->options['labelHeight'].",";
        $this->_html.="iconFolder: \"".$this->_baseUrl."/css/elements/".$this->options['iconFolder']."\",";
        $this->_html.="fadeTime:".$this->options['fadeTime'].",";
        $this->_html.="autoSlide:".$this->options['autoSlide'].",";
        $this->_html.="slideTimer:".$this->options['slideTimer'].",";
        $this->_html.="autoSize:".$this->options['autoSize'].",";
        $this->_html.="startTimer:".$this->options['startTimer']."";
        $this->_html.="};";
        $this->_html.="});";
        $this->_html.="</script>";
    }

    /*
     * setOption set default value if not defined already
     */
     private function setOption($optionKey,$defaultValue){
        if(!array_key_exists($optionKey, $this->options))
            $this->options[$optionKey] = $defaultValue;
     }
}
?>
